﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.OLE.Interop;
using Microsoft.VisualStudio;
using Widgetsphere.Generator.Common.GeneratorFramework;
using System.Windows.Forms;

namespace Widgetsphere.VsCodeTools
{
	public class CommandHandler : IOleCommandTarget
	{
		#region IOleCommandTarget Members
		public int Exec(ref Guid pguidCmdGroup, uint nCmdID, uint nCmdexecopt, IntPtr pvaIn, IntPtr pvaOut)
		{
			if (pguidCmdGroup == VSConstants.GUID_VSStandardCommandSet97)
			{
				// Process standard Visual Studio Commands
				switch (nCmdID)
				{
					case (uint)Microsoft.VisualStudio.VSConstants.VSStd97CmdID.Exit:
					default: return (int)(Microsoft.VisualStudio.OLE.Interop.Constants.OLECMDERR_E_NOTSUPPORTED);
				}
			}
			else if (pguidCmdGroup == GuidList.guidVsCodeToolsCmdSet)
			{
				//Determine if this guy can generate
				if (!GeneratorHelper.CanGenerate())
				{
					MessageBox.Show("The logged-in user does not have permissions to run the nHydrate plugin. This is generally due to the UAC (user access control) being enabled. If you have this feature enabled, please disable it or run Visual Studio as an administrator.", "Invalid Permissions", MessageBoxButtons.OK, MessageBoxIcon.Error);
					return VSConstants.S_OK;
				}

				switch (nCmdID)
				{
					// if we had commands specific to our editor, they would be processed here
					case 0x105:
						GeneratorHelper.ShowSettings();
						break;
					default:
						return (int)(Microsoft.VisualStudio.OLE.Interop.Constants.OLECMDERR_E_NOTSUPPORTED);
				}
			}
			else
				return (int)Microsoft.VisualStudio.OLE.Interop.Constants.OLECMDERR_E_UNKNOWNGROUP;

			return VSConstants.S_OK;

		}

		public int QueryStatus(ref Guid pguidCmdGroup, uint cCmds, OLECMD[] prgCmds, IntPtr pCmdText)
		{
			if ((prgCmds == null))
				return VSConstants.E_INVALIDARG;


			if (pguidCmdGroup == VSConstants.GUID_VSStandardCommandSet97)
			{
				// Process standard Commands
				switch (prgCmds[0].cmdID)
				{
					case (uint)Microsoft.VisualStudio.VSConstants.VSStd97CmdID.Exit:
					default: return (int)(Microsoft.VisualStudio.OLE.Interop.Constants.OLECMDERR_E_NOTSUPPORTED);
				}
			}
			else if (pguidCmdGroup == GuidList.guidVsCodeToolsCmdSet)
			{
				// Process our Commands
				switch (prgCmds[0].cmdID)
				{
					case 0x100:
						return (int)(Microsoft.VisualStudio.OLE.Interop.Constants.MSOCMDF_ENABLED);
					case 0x102:
						return (int)(Microsoft.VisualStudio.OLE.Interop.Constants.MSOCMDF_ENABLED);
					default:
						return (int)(Microsoft.VisualStudio.OLE.Interop.Constants.OLECMDERR_E_NOTSUPPORTED);
				}
			}
			else
				return (int)(Microsoft.VisualStudio.OLE.Interop.Constants.OLECMDERR_E_NOTSUPPORTED);

		}

		#endregion

	}
}